<?php 
/**
 * sfWidgetFormPropelChoice represents a choice widget for a model.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfWidgetFormPropelChoice.class.php 12803 2008-11-09 07:26:18Z fabien $
 */
class sfWidgetFormTreeComplex  extends sfWidgetFormTree
{
    /**
   * Configures the current widget.
   *
   * Available options:
   *
   *  * url:            The URL to call to get the choices to use (required)
   *  * config:         A JavaScript array that configures the JQuery autocompleter widget
   *  * value_callback: A callback that converts the value before it is displayed
   *
   * @param array $options     An array of options
   * @param array $attributes  An array of default HTML attributes
   *
   * @see sfWidgetForm
   */
    protected function configure($options = array(), $attributes = array())
    {
        parent::configure($options, $attributes);
    }

    /**
   * @param  string $name        The element name
   * @param  string $value       The date displayed in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
    public function render($name, $value = null, $attributes = array(), $errors = array())
    {
        $this->setOption('use_hidden_field' , false );
        $parent_html = parent::render( $name , null , $attributes , $errors );


        $id = $this->generateId( $name );

        $image_path = image_path('/images/blueArrow.jpg');
        $buttons = "
        <div class='buttonArrow tree-buttons'>
           <div class='blueArrow'><a href='#' onclick=\"return add_to_tree('{$id}')\"><img src='{$image_path}'> </a></div>
           <div class='clear'></div>
          </div>
        ";

        if( is_array($value) && !empty($value['html']) ){
            $value_html = $value['html'];
        }else{
            $value_html ="";
        }
        
       $post_fields  = "<input class='html_holder' type='text' name='{$name}[html]' value='$value_html'/>";
       //$post_fields  = "<textarea class='html_holder' name='{$name}[html]' >{$value_html}</textarea>";
        foreach ($this->getOption('fk_model_list') as $model){
             $model_value = array();
            if( is_array($value) && !empty($value['models']) && !empty($value['models'][$model]) && is_array($value['models'][$model]) ){
                $tmp = $value['models'][$model];
                foreach ($tmp as $v){
                    $model_value[$v] = $v;
                }
            }
            $widg = new sfWidgetFormChoice( array('choices'=>$model_value,'multiple'=>true) );
            $attr = array();
            $attr['model'] = $model;
            $attr['class'] = "models_holder model_{$model}";
            $post_fields .= $widg->render( "{$name}[models][{$model}][]" , $model_value , $attr );
            //$post_fields .= "<select multiple='multiple' model='{$model}' class='models_holder model_{$model}' name='{$name}[models][{$model}][]' /></select>";
        }



        $new_html = "<div id='{$id}_servent' class='tree tree-servent'>{$post_fields}<ul class='ul-root tree tree-default'>{$value_html}</ul></div>";
        $buttons_options ="
                      <div class='clear'></div>
                              <div class='options'>
                              <div class='loginhBtn'>
                    	<div class='loginBtn1'>
                          <div class='btnCont no-margin'>
                           <div class='btnContLC'></div><!--Btn-->
                            <div class='btnContText'><a href='#' onclick='return remove_from_tree(\"{$id}\"); '>Remove Item</a></div>
                            <!--Btn-->
                            <div class='btnContRC'></div><!--Btn-->
                        </div><!--Btn-->
                    </div><!--Btn-->
                    <div class='clear'></div>
                       </div> <!-- -->
                             <div class='loginhBtn second'>
                    	<div class='loginBtn1'>
                          <div class='btnCont no-margin'>
                           <div class='btnContLC'></div><!--Btn-->
                            <div class='btnContText'><a href='#' onclick='return clear_servent(\"{$id}\"); '>Clear Selection</a></div>
                            <!--Btn-->
                            <div class='btnContRC'></div><!--Btn-->
                        </div><!--Btn-->
                    </div><!--Btn-->
                    <div class='clear'></div>
                       </div> <!-- -->
                       <div class='clear'></div>
                       </div> <!--options -->
        ";
        
        $new_html = "
            <div class='selectionArea' style='float:right;display:inline'> 
                <div class='label'> My Selection </div>
                    <div class='selection' >{$new_html}
                        <div class='clear'></div>
                    </div>
                <div class='clear'></div>
              </div>
        ";


        $js = $this->getJavaScript2( $name , $value );

        /*        return "<div class='tree-up-lable'>{$hidden_field}{$html}</div>\n".
        "<div id='tree_{$id}' class='tree tree-default'></div>\n{$js}";
        */

        return $parent_html.$js.$buttons.$new_html.$buttons_options;

    }



    protected function getJavaScript2($name , $value){
        $id = $this->generateId($name);
        $url = $this->getOption('url');

        $script= <<<EOF
<script type="text/javascript">
var can_select_multi = true;




</script>
EOF;

        return $script;

    }

}